from fastapi import APIRouter, Request
from typing import List, Dict, Any

from ..services.provider import get_mcp_server_names
from ..services.provider import get_providers_data, get_provider_model_names
from scorpio.core.common import ScorpioConfig
from .dependencies import AppConfigDependency

router = APIRouter(prefix="/api", tags=["Basic API for APP Scorpio"])
@router.get("/mcp/servers", response_model=List[str])
async def get_mcp_servers(app_config: AppConfigDependency):  
    """
    获取所有MCP服务器名称
    
    参数:
        config: 应用配置依赖项（通过FastAPI依赖注入系统提供）
        
    返回:
        MCP服务器名称列表
    """
    return get_mcp_server_names(app_config.mcp_servers)

@router.get("/v1/mcp/servers", response_model=List[str])
async def get_v1_mcp_servers(
    req: Request,
):  
    # app_config = req.app.state.config
    app_config: ScorpioConfig = req.app.state.config
    
    #print(f'v1 app_config={app_config}')
    mcp_servers=list(app_config.mcp_servers.keys())
    return mcp_servers

# 原始数据（可迁移到数据库或配置文件）
"""
models = [
    {"provider": "OpenAI","model": "gpt-3.5", "title": "gpt-3.5"},
    {"provider": "OpenAI","model": "gpt-4.0", "title": "gpt-4.0"},
    {"provider": "Ollama","model": "qwen3:0.6b", "title": "qwen3:0.6b"},
]
"""
@router.get("/models", response_model=Dict)
async def get_models_by_provider(app_config: AppConfigDependency):
    #app_config=router.dependencies[0].dependency() # type: ignore
    models=get_provider_model_names(app_config.models)
    #return get_models_data(app_config.models) # 通过服务函数处理数据
    return models

@router.get("/models/detail", response_model=List[Any])
async def get_models(app_config: AppConfigDependency):
    # app_config=router.dependencies[0].dependency() # type: ignore
    models=[]
    for m in app_config.models:
        model=m.model_dump(exclude_unset=True)
        models.append(model)
    return models

@router.get("/providers", response_model=Any)
async def get_providers(app_config: AppConfigDependency):
    #app_config=router.dependencies[0].dependency() # type: ignore
    return get_providers_data(app_config.models)